/*
 *  author: Honrun
 */
#include "stm32f4xx_hal.h"
#include "stdio.h"
#include "DevicesTimer.h"


/* Timer handler declaration */
TIM_HandleTypeDef   g_typeTim1Handle = {0};
TIM_HandleTypeDef   g_typeTim2Handle = {0};
TIM_HandleTypeDef   g_typeTim3Handle = {0};
TIM_HandleTypeDef   g_typeTim4Handle = {0};
TIM_HandleTypeDef   g_typeTim5Handle = {0};
TIM_HandleTypeDef   g_typeTim6Handle = {0};
TIM_HandleTypeDef   g_typeTim7Handle = {0};
TIM_HandleTypeDef   g_typeTim8Handle = {0};


int8_t cTimer5Init(void)
{
    /* TIMx Peripheral clock enable */
    __HAL_RCC_TIM5_CLK_ENABLE();

    /* Set TIMx instance */
    g_typeTim5Handle.Instance = TIM5;
    g_typeTim5Handle.Init.Period = 10000 - 1;
    g_typeTim5Handle.Init.Prescaler = (SystemCoreClock / 2 / 1000000) - 1;
    g_typeTim5Handle.Init.ClockDivision = 0;
    g_typeTim5Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
    g_typeTim5Handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
    if(HAL_TIM_Base_Init(&g_typeTim5Handle) != HAL_OK)
    {
        /* Initialization Error */
        printf("HAL_TIM_Base_Init error.\r\n");
    }

    /* Enable and set EXTI Line0 Interrupt to the lowest priority */
    HAL_NVIC_SetPriority(TIM5_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(TIM5_IRQn);

    /* Start the TIM Base generation in interrupt mode */
    if(HAL_TIM_Base_Start_IT(&g_typeTim5Handle) != HAL_OK)
    {
        /* Starting Error */
        printf("HAL_TIM_Base_Start_IT error.\r\n");
    }

    return 0;
}

int8_t cTimer6Init(void)
{
    /* TIMx Peripheral clock enable */
    __HAL_RCC_TIM6_CLK_ENABLE();

    /* Set TIMx instance */
    g_typeTim6Handle.Instance = TIM6;
    g_typeTim6Handle.Init.Period = 65536 - 1;
    g_typeTim6Handle.Init.Prescaler = (SystemCoreClock / 2 / 1000000) - 1;
    g_typeTim6Handle.Init.ClockDivision = 0;
    g_typeTim6Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
    g_typeTim6Handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
    if(HAL_TIM_Base_Init(&g_typeTim6Handle) != HAL_OK)
    {
        /* Initialization Error */
        printf("HAL_TIM_Base_Init error.\r\n");
    }

    /* Enable and set EXTI Line0 Interrupt to the lowest priority */
    HAL_NVIC_SetPriority(TIM6_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(TIM6_IRQn);

    /* Start the TIM Base generation in interrupt mode */
    if(HAL_TIM_Base_Start_IT(&g_typeTim6Handle) != HAL_OK)
    {
        /* Starting Error */
        printf("HAL_TIM_Base_Start_IT error.\r\n");
    }

    return 0;
}
